<?php
namespace Admin\Controller;
class UserController extends BaceController{
/*  
 * User用户表
 * add添加用户
 * oper查询 
 * dalete删除
 * edit编辑
 * */
    
    public function add(){
       $name=$_POST['name'];
       $email=$_POST['email'];
       $role_ids=$_POST['role_ids'];
       if (mb_strlen($name)<1 || mb_strlen($name)>15){
           $this->ajaxReturn(array('pass'=>1,'msg'=>'请输入合法的姓名！'));
       }
       if(preg_match("/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/",$email)){
       }else{
           $this->ajaxReturn(array("pass"=>0,'msg'=>'邮箱格式不正确!    xx@.xx.com'));
       }
       $UserOb=M('User');
        $cRe=$UserOb->create();
        $re= $UserOb->add();
            if ($re) {        //如果添加成功，那就保存角色用用户的关系                 
                if ($role_ids) {
                   foreach ($role_ids as $role_id) {
                       $user_role=M('User_role');
                       $time=date('Y-m-d H:i:s',time());
                       $data=array('uid'=>$re,'role_id'=>$role_id,'created_time'=>$time);
                       $user_role->add($data);
                   }
                }
               $this->ajaxReturn(array('pass'=>1,'msg'=>'添加成功'));
            }
         }
    public function user_list(){
        $UserOb=D('User');          //分页
        $num=$UserOb->count();
        $this->assign('num',$num);
        $pageSize=5;
        $page= new \Think\Page($num,$pageSize);
        $pageStr=$page->show();
        $start=$page->firstRow;
        $this->assign('pageStr',$pageStr);
        $arr=$UserOb->order('id desc')
               ->limit($start,$pageSize)
               ->select();
        $this->assign('arr',$arr);
        
        
        
        
        
        $this->display();
    }
    function delete(){    //删除
        $where['id']=array('in',$_POST['id']);
        $RoleOb=M('User');
        $re=$RoleOb->where($where)->delete();
        if ($re){
            $data['a']='pass';
        };
        $this->ajaxReturn($data);
    }
    function user_alter(){      //编辑
        $name=$_GET['name'];
        $this->assign('name',$name);
        $email=$_GET['email'];
        $this->assign('email',$email);
        $id=$_GET['id'];
        $this->assign('id',$id);
   
     if(!empty($_POST)){
         if ($_POST['role_ids']!='') {    //编辑用户角色关系
             $user_roleOb=M('User_role');
             $role_ids=$_POST['role_ids'];
             $id=$_POST['id'];
             foreach ($role_ids as $role_id) {
                 $data=array('role_id'=>$role_id);
                 $user_roleOb->where("uid=$id")
                 ->data($data)
                 ->save();
             }
         }else {
                 $data=array('role_id'=>$role_ids);
                 $user_roleOb->where("uid=$id")
                 ->data($data)
                 ->save();
         }
         //编辑用户
            $userOb=M('User');
            $name=$_POST['name'];
            $email=$_POST['email'];
            if(mb_strlen($name)<1 || mb_strlen($name)>15) {
                $this->ajaxReturn(array('pass'=>1,'msg'=>'请输入合法的名称  ！'));
            }
            if (preg_match("/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/", $email)) {
               
            }else {
                $this->ajaxReturn(array('pass'=>1,'msg'=>'邮箱格式不正确  ！  xx@xx.com!!'));
            }
            $cRe=$userOb->create();
            if ($cRe){
                $id=$_POST['id'];
                $re=$userOb->where("id=$id")->save();
                $this->ajaxReturn(array('pass'=>2,'msg'=>'操作成功！'));
            }
        }
        
       //取出所以的角色
       $roleOb=M('Role');
       $arr=$roleOb->where('status=1')->select();
       $this->assign('arr',$arr);
       //取出所有已分配的角色
       if (!empty($id)) {
       $user_role=M('user_role');
       $user_role_id=$user_role->where("uid=$id")
                               ->select();
       foreach ($user_role_id as $v ){
           $role_id=$v['role_id'];
       }
       $this->assign('role_id',$role_id);
       }
       $this->display();
        
    }
    
    function user_add(){ 
        //取出所有的角色 
        $Role=M('Role');
        $arr=$Role->where('status=1')->select();
        $this->assign('arr',$arr);
        $this->display();
    }
  
}